package openvpn

import (
	"fmt"
	log "github.com/sirupsen/logrus"
	"os"
	"vpn/app/global"
	"vpn/app/utils/shell"
	"vpn/template"
)

func SaveEnvConf(subnet string, mask string, clientConnEnable bool, password string) {
	var clientConnOption string
	if clientConnEnable {
		clientConnOption = "client-to-client"
	} else {
		clientConnOption = "#client-to-client"
	}
	envConfPath := global.OpenvpnConf.EnvConfPath
	content := fmt.Sprintf("OPENVPN_SUBNET=%s\nOPENVPN_NETMASK=%s\nOPENVVPN_CLIENT_CONN_ENABLE=%s\nOPENVPN_PASSWORD=%s", subnet, mask, clientConnOption, password)
	err := os.WriteFile(envConfPath, []byte(content), 0755)
	if err != nil {
		log.Error("Failed to write to SaveConfShellScript file: %v", err)
	}
}

func SaveConf(subnet string, mask string, clientConnEnable bool) {
	SaveEnvConf(subnet, mask, clientConnEnable, "")
	shellScriptPath := global.OpenvpnConf.InstallScriptPath
	createShellScriptTemplate(shellScriptPath, template.SaveConfShellScript, true)
	shell.ShellFileExec(shellScriptPath)
}
